package com.hansming.bss.modules.sfc.util;

import java.awt.image.BufferedImage;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

public class CommUtil {
	
	
	
	//创建文件夹
	public static boolean createFolder(String folderPath){
        boolean ret = true;
        try {
            File myFilePath = new File(folderPath);
            if ((!myFilePath.exists()) && (!myFilePath.isDirectory())){
                ret = myFilePath.mkdirs();
                if (!ret)
                    System.out.println("创建文件夹出错");
            }
        } catch (Exception e){
            System.out.println("创建文件夹出错");
            ret = false;
        }

        return ret;
    }

	
	
	 /**
     * 保存文件到服务器
     * @param request
     * @param filePath
     * @param saveFilePathName
     * @param saveFileName
     * @param extendes
     * @return
     * @throws IOException
     */
    public static Map saveFileToServer(HttpServletRequest request, String filePath, String saveFilePathName, String saveFileName, String[] extendes)
    throws IOException {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
        CommonsMultipartFile file = (CommonsMultipartFile)multipartRequest.getFile(filePath);
        Map map = new HashMap();
        if ((file != null) && (!file.isEmpty())){
            String extend = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1).toLowerCase();
            if ((saveFileName == null) || (saveFileName.trim().equals(""))){
                saveFileName = UUID.randomUUID().toString() + "." + extend;
            }
            if (saveFileName.lastIndexOf(".") < 0){
                saveFileName = saveFileName + "." + extend;
            }
            float fileSize = Float.valueOf((float)file.getSize()).floatValue();
            List errors = new ArrayList();
            boolean flag = true;
            if (extendes != null){
                for (String s : extendes){
                    if (extend.toLowerCase().equals(s)){
                        flag = true;
                    }
                }
            }
            if (flag){
                File path = new File(saveFilePathName);
                if (!path.exists()){
                    path.mkdir();
                }
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
                DataOutputStream out = new DataOutputStream(
                    new FileOutputStream(saveFilePathName + File.separator + saveFileName));
                InputStream is = null;
                try {
                    is = file.getInputStream();
                    int size = (int)fileSize;
                    byte[] buffer = new byte[size];
                    while (is.read(buffer) > 0)
                        out.write(buffer);
                } catch (IOException exception){
                    exception.printStackTrace();
                } finally {
                    if (is != null){
                        is.close();
                    }
                    if (out != null){
                        out.close();
                    }
                }
                if (isImg(extend)){
                    File img = new File(saveFilePathName + File.separator + saveFileName);
                    try {
                        BufferedImage bis = ImageIO.read(img);
                        int w = bis.getWidth();
                        int h = bis.getHeight();
                        map.put("width", Integer.valueOf(w));
                        map.put("height", Integer.valueOf(h));
                    } catch (Exception localException){
                    }
                }
                map.put("mime", extend);
                map.put("fileName", saveFileName);
                map.put("fileSize", Float.valueOf(fileSize));
                map.put("error", errors);
                map.put("oldName", file.getOriginalFilename());
            }else{
                errors.add("不允许的扩展名");
            }
        }else{
            map.put("width", Integer.valueOf(0));
            map.put("height", Integer.valueOf(0));
            map.put("mime", "");
            map.put("fileName", "");
            map.put("fileSize", Float.valueOf(0.0F));
            map.put("oldName", "");
        }

        return map;
    }
    
    
    
    //是否图片
    public static boolean isImg(String extend){
        boolean ret = false;
        List<String> list = new ArrayList<String>();
        list.add("jpg");
        list.add("jpeg");
        list.add("bmp");
        list.add("gif");
        list.add("png");
        list.add("tif");
        for (String s : list){
            if (s.equals(extend))
                ret = true;
        }

        return ret;
    }
	
    public static int null2Int(Object s){
        int v = 0;
        if (s != null)
            try {
                v = Integer.parseInt(s.toString());
            } catch (Exception localException){
            }

        return v;
    }

    public static float null2Float(Object s){
        float v = 0.0F;
        if (s != null)
            try {
                v = Float.parseFloat(s.toString());
            } catch (Exception localException){
            }

        return v;
    }

    public static double null2Double(Object s){
        double v = 0.0D;
        if (s != null)
            try {
                v = Double.parseDouble(null2String(s));
            } catch (Exception localException){
            }

        return v;
    }

    public static boolean null2Boolean(Object s){
        boolean v = false;
        if (s != null)
            try {
                v = Boolean.parseBoolean(s.toString());
            } catch (Exception localException){
            }

        return v;
    }

    public static String null2String(Object s){
        return s == null ? "" : s.toString().trim();
    }

    public static Long null2Long(Object s){
        Long v = Long.valueOf(-1L);
        if (s != null)
            try {
                v = Long.valueOf(Long.parseLong(s.toString()));
            } catch (Exception localException){
            }

        return v;
    }
	
	
}
